import unittest
from compare import Compare
from tree import Tree
from node import Node
from displaytree import DisplayTree
from tedtree import TEDTree

class TestCompare(unittest.TestCase):

    def setUp(self):
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeA, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeE = Node("E", nodeC, style=Node.NORM)
        nodeF = Node("F", nodeC, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeA.setChild2(nodeC)
        nodeB.setChild2(nodeD)
        nodeC.setChild1(nodeE)
        nodeC.setChild2(nodeF)
        self.tree1 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeE = Node("E", root, style=Node.NORM)
        nodeC = Node("C", nodeE, style=Node.NORM)
        nodeD = Node("D", nodeE, style=Node.NORM)
        nodeA = Node("A", nodeD, style=Node.NORM)
        nodeB = Node("B", nodeD, style=Node.NORM)
        nodeF = Node("F", nodeC, style=Node.NORM)
        root.setChild1(nodeE)
        nodeD.setChild1(nodeB)
        nodeD.setChild2(nodeA)
        nodeE.setChild1(nodeD)
        nodeE.setChild2(nodeC)
        nodeC.setChild2(nodeF)
        self.tree2 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeE = Node("E", root, style=Node.NORM)
        nodeC = Node("C", nodeE, style=Node.NORM)
        nodeD = Node("D", nodeE, style=Node.NORM)
        nodeA = Node("A", nodeD, style=Node.NORM)
        nodeB = Node("B", nodeD, style=Node.NORM)
        nodeF = Node("F", nodeC, style=Node.NORM)
        nodeG = Node("G", nodeF, style=Node.NORM)
        nodeH = Node("H", nodeF, style=Node.NORM)
        root.setChild1(nodeE)
        nodeD.setChild1(nodeB)
        nodeD.setChild2(nodeA)
        nodeE.setChild1(nodeD)
        nodeE.setChild2(nodeC)
        nodeC.setChild2(nodeF)
        nodeF.setChild1(nodeG)
        nodeF.setChild2(nodeH)
        self.tree3 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeH= Node("H", root, style=Node.NORM)
        nodeC = Node("C", nodeH, style=Node.NORM)
        nodeD = Node("D", nodeH, style=Node.NORM)
        nodeA = Node("A", nodeD, style=Node.NORM)
        nodeB = Node("B", nodeD, style=Node.NORM)
        nodeF = Node("F", nodeC, style=Node.NORM)
        nodeG = Node("G", nodeF, style=Node.NORM)
        nodeE = Node("E", nodeF, style=Node.NORM)
        root.setChild1(nodeH)
        nodeD.setChild1(nodeB)
        nodeD.setChild2(nodeA)
        nodeH.setChild1(nodeD)
        nodeH.setChild2(nodeC)
        nodeC.setChild2(nodeF)
        nodeF.setChild1(nodeG)
        nodeF.setChild2(nodeE)
        self.tree4 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeA.setChild2(nodeK)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeD)
        self.tree5 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeD)
        self.tree6 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeH = Node("H", nodeK, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeA.setChild2(nodeK)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeD)
        nodeK.setChild2(nodeH)
        self.tree7 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeG = Node("G", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeG, style=Node.NORM)
        nodeD = Node("D", nodeG, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeG)
        nodeA.setChild2(nodeK)
        nodeG.setChild1(nodeC)
        nodeG.setChild2(nodeD)
        self.tree8 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeB = Node("B", root, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeA = Node("A", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeA, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        root.setChild1(nodeB)
        nodeA.setChild1(nodeD)
        nodeA.setChild2(nodeK)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeA)
        self.tree9 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeK = Node("K", root, style=Node.NORM)
        nodeA = Node("A", nodeK, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        root.setChild1(nodeK)
        nodeK.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeD)
        self.tree10 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeB.setChild1(nodeC)
        nodeB.setChild2(nodeD)
        self.tree11 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeB = Node("B", nodeK, style=Node.NORM)
        nodeE = Node("E", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeK)
        nodeK.setChild1(nodeB)
        nodeB.setChild1(nodeE)
        nodeB.setChild2(nodeD)
        self.tree12 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeB = Node("B", nodeK, style=Node.NORM)
        nodeE = Node("E", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeG = Node("G", nodeD, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeK)
        nodeK.setChild1(nodeB)
        nodeB.setChild1(nodeE)
        nodeB.setChild2(nodeD)
        nodeD.setChild2(nodeG)
        self.tree13 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeK)
        nodeA.setChild2(nodeB)
        nodeK.setChild1(nodeC)
        nodeK.setChild2(nodeD)
        self.tree14 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeK = Node("K", root, style=Node.NORM)
        nodeG = Node("G", nodeK, style=Node.NORM)
        nodeU = Node("U", nodeK, style=Node.NORM)
        nodeQ = Node("Q", nodeG, style=Node.NORM)
        nodeJ = Node("J", nodeG, style=Node.NORM)
        root.setChild1(nodeK)
        nodeK.setChild1(nodeG)
        nodeK.setChild2(nodeU)
        nodeG.setChild1(nodeQ)
        nodeG.setChild2(nodeJ)
        self.tree15 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeK = Node("K", root, style=Node.NORM)
        nodeQ = Node("Q", nodeK, style=Node.NORM)
        nodeD = Node("D", nodeK, style=Node.NORM)
        nodeJ = Node("J", nodeD, style=Node.NORM)
        nodeU = Node("U", nodeD, style=Node.NORM)
        root.setChild1(nodeK)
        nodeK.setChild1(nodeQ)
        nodeK.setChild2(nodeD)
        nodeD.setChild1(nodeJ)
        nodeD.setChild2(nodeU)
        self.tree16 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeK = Node("(K,K)", root, style=Node.NORM)
        nodeG = Node("(G,\lambda)", nodeK, style=Node.NORM)
        nodeD = Node("(\lambda, D)", nodeK, style=Node.NORM)
        nodeQ = Node("(Q, Q)", nodeG, style=Node.NORM)
        nodeFJ = Node("(J, \lambda)", nodeG, style=Node.NORM)
        nodeSJ = Node("(\lambda, J)", nodeD, style=Node.NORM)
        nodeU = Node("(U, U)", nodeD, style=Node.NORM)
        root.setChild1(nodeK)
        nodeK.setChild1(nodeG)
        nodeK.setChild2(nodeD)
        nodeG.setChild1(nodeQ)
        nodeG.setChild2(nodeFJ)
        nodeD.setChild1(nodeSJ)
        nodeD.setChild2(nodeU)
        self.tree17 = Tree(root)
        #print self.tree3.toString()
        """
        for node in self.tree2.iterator(root):
            print "Node: " + node.getLabel()
        """
        self.compare = Compare(self.tree1, self.tree2)
        self.compare_labels = Compare(self.tree1, self.tree3)
        self.compare_paths = Compare(self.tree1, self.tree4)

    def test_same_labels_move_root(self):
        root = self.tree1.getRoot().getChild1()
        sub = self.tree2
        rootSub = self.tree2.getRoot().getChild1()
        #print self.tree1.toString()
        distance = self.compare.move_root(root, sub, rootSub)

        self.assertTrue(sub.getRoot().getChild1().getLabel() == "A")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getLabel() == "B")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getLabel() == "C")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getChild2().getLabel() == "D")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild1().getLabel() == "E")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getLabel() == "F")
        self.assertTrue(distance == 4)

    def test_different_labels_move_root(self):
        root = self.tree1.getRoot().getChild1()
        sub = self.tree3
        print self.tree3.toString()
        rootSub = self.tree3.getRoot().getChild1()
        distance = self.compare.move_root(root, sub, rootSub)

        self.assertTrue(sub.getRoot().getChild1().getLabel() == "A")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getLabel() == "B")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getLabel() == "C")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getChild2().getLabel() == "D")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild1().getLabel() == "E")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getLabel() == "F")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getChild1().getLabel() == "G")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getChild2().getLabel() == "H")
        self.assertTrue(distance == 4)

    def test_different_labels_path_move_root(self):
        root = self.tree1.getRoot().getChild1()
        sub = self.tree4
        print self.tree4.toString()
        rootSub = self.tree4.getRoot().getChild1()
        distance = self.compare.move_root(root, sub, rootSub)
        print self.tree4.toString()

        self.assertTrue(sub.getRoot().getChild1().getLabel() == "A")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getLabel() == "B")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getLabel() == "C")
        self.assertTrue(sub.getRoot().getChild1().getChild1().getChild2().getLabel() == "D")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild1().getLabel() == "E")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getLabel() == "F")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getChild1().getLabel() == "G")
        self.assertTrue(sub.getRoot().getChild1().getChild2().getChild2().getChild2().getLabel() == "H")
        self.assertTrue(distance == 4)

    def test_display_tree(self):
        DisplayTree().getTree(self.tree1, "test1")
        DisplayTree().getTree(self.tree2, "test2")
        DisplayTree().getTree(self.tree3, "test3")
        DisplayTree().getTree(self.tree4, "test4")
        DisplayTree().getTree(self.tree5, "Original")
        DisplayTree().getTree(self.tree6, "Delete")
        DisplayTree().getTree(self.tree7, "Insert")
        DisplayTree().getTree(self.tree8, "Relabel")
        DisplayTree().getTree(self.tree9, "RightRotation")
        DisplayTree().getTree(self.tree10, "LeftRotation")
        DisplayTree().getTree(self.tree11, "(b)")
        DisplayTree().getTree(self.tree12, "(c)")
        DisplayTree().getTree(self.tree13, "(d)")
        DisplayTree().getTree(self.tree14, "Switch")
        DisplayTree().getTree(self.tree15, "one")
        DisplayTree().getTree(self.tree16, "two")
        DisplayTree().getTree(self.tree17, "result")

    def test_import_ted_tree(self):
        tree = TEDTree("C:\\Users\\Charles\\Documents\\jira-reporting\\Thesis\\Trees\\Test0G.txt")
        tree.parseBinaryTree()
        tree = tree.getTree()
        print tree.toString()
        DisplayTree().getTree(tree, "New1")
        

if __name__ == '__main__':
    unittest.main()
